package com.it.machine.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.it.common.annotation.Log;
import com.it.common.core.controller.BaseController;
import com.it.common.core.domain.AjaxResult;
import com.it.common.enums.BusinessType;
import com.it.machine.domain.MachineActivation;
import com.it.machine.service.IMachineActivationService;
import com.it.common.utils.poi.ExcelUtil;
import com.it.common.core.page.TableDataInfo;
import com.it.common.annotation.Anonymous;

/**
 * 激活电脑Controller
 * 
 * @author 云淡风轻
 * @date 2025-04-28
 */
@RestController
@RequestMapping("/machine/activation")
public class MachineActivationController extends BaseController
{
    @Autowired
    private IMachineActivationService machineActivationService;

    /**
     * 查询激活电脑列表
     */
    @PreAuthorize("@ss.hasPermi('machine:activation:list')")
    @GetMapping("/list")
    public TableDataInfo list(MachineActivation machineActivation)
    {
        startPage();
        List<MachineActivation> list = machineActivationService.selectMachineActivationList(machineActivation);
        return getDataTable(list);
    }

    /**
     * 导出激活电脑列表
     */
    @PreAuthorize("@ss.hasPermi('machine:activation:export')")
    @Log(title = "激活电脑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MachineActivation machineActivation)
    {
        List<MachineActivation> list = machineActivationService.selectMachineActivationList(machineActivation);
        ExcelUtil<MachineActivation> util = new ExcelUtil<MachineActivation>(MachineActivation.class);
        util.exportExcel(response, list, "激活电脑数据");
    }

    /**
     * 获取激活电脑详细信息
     */
    @PreAuthorize("@ss.hasPermi('machine:activation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(machineActivationService.selectMachineActivationById(id));
    }

    /**
     * 新增激活电脑
     */
    @PreAuthorize("@ss.hasPermi('machine:activation:add')")
    @Log(title = "激活电脑", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MachineActivation machineActivation)
    {
        return toAjax(machineActivationService.insertMachineActivation(machineActivation));
    }

    /**
     * 修改激活电脑
     */
    @PreAuthorize("@ss.hasPermi('machine:activation:edit')")
    @Log(title = "激活电脑", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MachineActivation machineActivation)
    {
        return toAjax(machineActivationService.updateMachineActivation(machineActivation));
    }

    /**
     * 删除激活电脑
     */
    @PreAuthorize("@ss.hasPermi('machine:activation:remove')")
    @Log(title = "激活电脑", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(machineActivationService.deleteMachineActivationByIds(ids));
    }
    

    
    /**
     * 激活验证接口
     * 供其他软件调用，验证并激活机器
     */
    @Anonymous
    @PostMapping("/verify")
    public AjaxResult verifyActivation(@RequestBody VerifyActivationRequest request) {
        String proId = request.getProId();
        String machineId = request.getMachineId();
        String activeCode = request.getActiveCode();
        
        int result = machineActivationService.verifyAndActivate(proId, machineId, activeCode);
        switch (result) {
            case 0:
                return success("激活成功");
            case 1:
                return error("激活码不存在");
            case 2:
                return error("激活码已使用");
            case 3:
                return error("机器码不匹配");
            case 5:
                return error("激活码已过期");
            default:
                return error("激活失败");
        }
    }
    
    /**
     * 单独的验证接口
     * 供其他软件调用，仅验证机器是否已激活
     */
    @Anonymous
    @GetMapping("/validate")
    public AjaxResult validateMachine(@RequestParam String proId, @RequestParam String machineId) {
        // service层已实现实时检查过期时间逻辑，会自动更新status值
        MachineActivation activation = machineActivationService.selectMachineActivationByProIdAndMachineId(proId, machineId);
        if (activation == null) {
            return error("未找到激活记录");
        }
        
        // 检查激活状态
        if (activation.getStatus() == 1) {
            return success("已激活");
        } else if (activation.getStatus() == 2) {
            return error("已过期");
        } else if (activation.getStatus() == 3) {
            return error("已禁用");
        }
        
        return error("验证失败");
    }
    
    /**
     * 激活验证请求参数
     */
    static class VerifyActivationRequest {
        private String proId; // 项目ID
        private String machineId; // 机器码
        private String activeCode; // 激活码
        
        public String getProId() {
            return proId;
        }
        
        public void setProId(String proId) {
            this.proId = proId;
        }
        
        public String getMachineId() {
            return machineId;
        }
        
        public void setMachineId(String machineId) {
            this.machineId = machineId;
        }
        
        public String getActiveCode() {
            return activeCode;
        }
        
        public void setActiveCode(String activeCode) {
            this.activeCode = activeCode;
        }
    }
    
    /**
     * 禁用机器激活
     */
    @PreAuthorize("@ss.hasPermi('machine:activation:disable')")
    @Log(title = "禁用机器激活", businessType = BusinessType.UPDATE)
    @PutMapping("/disable/{id}")
    public AjaxResult disable(@PathVariable("id") Long id)
    {
        return toAjax(machineActivationService.disableMachineActivation(id));
    }
    
    /**
     * 启用机器激活
     */
    @PreAuthorize("@ss.hasPermi('machine:activation:enable')")
    @Log(title = "启用机器激活", businessType = BusinessType.UPDATE)
    @PutMapping("/enable/{id}")
    public AjaxResult enable(@PathVariable("id") Long id)
    {
        return toAjax(machineActivationService.enableMachineActivation(id));
    }
}
